
//
//  CStousuCOnViewController.m
//  ZSYH
//
//  Created by 主事丫环 on 2019/4/29.
//  Copyright © 2019 魏堰青. All rights reserved.
//

#import "CStousuCOnViewController.h"
#import "CSTSConListTableViewCell.h"
#import "CStousuConLisModel.h"
#import "DISAcomplaintLsitModel.h"

@interface CStousuCOnViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *mydateSource;
@property(nonatomic,strong)NSDictionary *mydic;
@property(nonatomic,strong)DISAcomplaintLsitModel *headModel;

@end

@implementation CStousuCOnViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    self.title = @"投诉详情";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
    self.headModel = [[DISAcomplaintLsitModel alloc]init];
    [self.myTableView reloadData];
    
    [self request];
    // Do any additional setup after loading the view.
}

-(void)shottabfoot{
    
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 200)];
    header.userInteractionEnabled = YES;
    CGFloat leftLBwith =57+8;
    CGFloat leftdis =10;
    CGFloat leftLBhei =32;
    
    CGFloat rightwiX = KWIDTH-130;
    
    UILabel *onbg = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, KWIDTH-20, 40)];
    onbg.backgroundColor = [UIColor colorWithHexString:@"#FAFAFA"];
    [header addSubview:onbg];
    
    UILabel *left0 = [[UILabel alloc]initWithFrame:CGRectMake(leftdis, 0, leftLBwith, 40)];
    left0.font = FontSize(14);
    left0.textColor = K666666;
    left0.text = @"投诉主题";
    [onbg addSubview:left0];
    UILabel *center0 = [[UILabel alloc]initWithFrame:CGRectMake(left0.right, 0,rightwiX-20-left0.right, 40)];
    center0.font = FontSize(16);
    center0.textColor = zhutiColor;
    center0.text = _headModel.title;
    [onbg addSubview:center0];
    
    UILabel *right0 = [[UILabel alloc]initWithFrame:CGRectMake(rightwiX-20, 0, 130, 40)];
    right0.font = FontSize(12);
    right0.textColor = zhutiColor;
    right0.text = [NSString stringWithFormat:@"%@",_headModel.createDate];
    [onbg addSubview:right0];
    
    UILabel *line0 = [[UILabel alloc]initWithFrame:CGRectMake(10, onbg.bottom, KWIDTH-20, 1)];
    line0.backgroundColor = [UIColor colorWithHexString:@"#DBDBDB"];
    [header addSubview:line0];
    UILabel *bgtwo = [[UILabel alloc]initWithFrame:CGRectMake(10, line0.bottom, KWIDTH-20, 100)];
    bgtwo.backgroundColor = [UIColor whiteColor];
    [header addSubview:bgtwo];
    bgtwo.userInteractionEnabled = YES;
    
    UILabel *left1 = [[UILabel alloc]initWithFrame:CGRectMake(leftdis, 0, leftLBwith, leftLBhei)];
    left1.font = FontSize(14);
    left1.textColor = K666666;
    left1.text = @"投诉人";
    [bgtwo addSubview:left1];
    UILabel *center1 = [[UILabel alloc]initWithFrame:CGRectMake(left0.right, 0,rightwiX-20-left0.right, leftLBhei)];
    center1.font = FontSize(16);
    center1.textColor = [UIColor colorWithHexString:@"#232620"];
    center1.text = _headModel.user_name;
    [bgtwo addSubview:center1];
    
    UIButton *right0but = [UIButton buttonWithType:(UIButtonTypeCustom)];
    right0but.frame =CGRectMake(rightwiX-20, 0, 125, leftLBhei);
    [right0but setImage:imgname(@"newPhone") forState:(UIControlStateNormal)];
    [bgtwo addSubview:right0but];
    [right0but setTitleColor:[UIColor colorWithHexString:@"#FFF6A533"] forState:(UIControlStateNormal)];
    [right0but setTitle:@"电话联系" forState:(UIControlStateNormal)];
    right0but.titleLabel.font = FontSize(14);
    //[right0but setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 110)];
    [right0but addTarget:self action:@selector(callAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UILabel *line1 = [[UILabel alloc]initWithFrame:CGRectMake(10, left1.bottom, KWIDTH-30, 1)];
    line1.backgroundColor = [UIColor colorWithHexString:@"#DBDBDB"];
    [bgtwo addSubview:line1];
    
    UILabel *left2 = [[UILabel alloc]initWithFrame:CGRectMake(leftdis, line1.bottom, leftLBwith, leftLBhei)];
    left2.font = FontSize(14);
    left2.textColor = K666666;
    left2.text = @"被投诉人";
    [bgtwo addSubview:left2];
    UILabel *center2 = [[UILabel alloc]initWithFrame:CGRectMake(left0.right, line1.bottom,rightwiX-20-left0.right, leftLBhei)];
    center2.font = FontSize(16);
    center2.textColor = [UIColor colorWithHexString:@"#232620"];
    center2.text = _headModel.nickname;
    [bgtwo addSubview:center2];
    UIButton *right1but = [UIButton buttonWithType:(UIButtonTypeCustom)];
    right1but.frame =CGRectMake(rightwiX-20, line1.bottom, 125, leftLBhei);
    [right1but setImage:imgname(@"newPhone") forState:(UIControlStateNormal)];
    [bgtwo addSubview:right1but];
    [right1but setTitleColor:[UIColor colorWithHexString:@"#FFF6A533"] forState:(UIControlStateNormal)];
    [right1but setTitle:@"电话联系" forState:(UIControlStateNormal)];
    right1but.titleLabel.font = FontSize(14);
    //    [right1but setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 110)];
    [right1but addTarget:self action:@selector(right1butcallAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    CGFloat fff = left2.bottom;
    NSString *set = [USER_DEFAULT objectForKey:@"level"];
    if ([set integerValue] == 1) {
        UILabel *line11 = [[UILabel alloc]initWithFrame:CGRectMake(10, left2.bottom, KWIDTH-30, 1)];
        line11.backgroundColor = [UIColor colorWithHexString:@"#DBDBDB"];
        [bgtwo addSubview:line11];
        UILabel *left22 = [[UILabel alloc]initWithFrame:CGRectMake(leftdis, line11 .bottom, leftLBwith, leftLBhei)];
        left22.font = FontSize(14);
        left22.textColor = K666666;
        left22.text = @"所属区域";
        [bgtwo addSubview:left22];
        UILabel *center22 = [[UILabel alloc]initWithFrame:CGRectMake(left0.right, line11.bottom,rightwiX-20-left0.right, leftLBhei)];
        center22.font = FontSize(16);
        center22.textColor = [UIColor colorWithHexString:@"#232620"];
        center22.text = [_mydic objectForKey:@"area_name"];
        [bgtwo addSubview:center22];
        
        fff =left22.bottom;
    }
    
    UILabel *line2 = [[UILabel alloc]initWithFrame:CGRectMake(10, fff, KWIDTH-30, 1)];
    line2.backgroundColor = [UIColor colorWithHexString:@"#DBDBDB"];
    [bgtwo addSubview:line2];
    
    UILabel *left3 = [[UILabel alloc]initWithFrame:CGRectMake(leftdis, line2.bottom, leftLBwith, leftLBhei)];
    left3.font = FontSize(14);
    left3.textColor = K666666;
    left3.text = @"投诉说明";
    [bgtwo addSubview:left3];
    
    CGFloat fffh = [NSString heightWithWidth:KWIDTH-left3.right-19-8 font:14 text:_headModel.content];
    if (fffh < leftLBhei) {
        fffh = leftLBhei;
    }
    UILabel *center3 = [[UILabel alloc]initWithFrame:CGRectMake(left0.right, line2.bottom,KWIDTH-left3.right-19-8, fffh)];
    center3.font = FontSize(14);
    //    center3.backgroundColor= [UIColor cyanColor];
    center3.textColor = [UIColor colorWithHexString:@"#232620"];
    center3.text = _headModel.content;
    center3.numberOfLines = 0;
    [bgtwo addSubview:center3];
    bgtwo.height = center3.bottom;
    
    header.height = bgtwo.bottom;
    
    self.myTableView.tableHeaderView = header;
}

-(void)callAction:(UIButton *)but{
    [HFTools callMobilePhone:_headModel.phone];
}

-(void)right1butcallAction:(UIButton *)but{
    [HFTools callMobilePhone:_headModel.repPhone];
}

-(void)request{
    [self.mydateSource removeAllObjects];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"id"] = NOTNIL(_idStr);
    [NetWorkTool POST:getComplaintInfo param:param success:^(id dic) {
        [DYModelMaker DY_makeModelWithDictionary:[dic objectForKey:@"data"] modelKeyword:@"" modelName:@""];
        [self.headModel setValuesForKeysWithDictionary:[dic objectForKey:@"data"]];
        
        self.mydic = [NSDictionary dictionaryWithDictionary:[dic objectForKey:@"data"]];
        [self shottabfoot];

        if ([self.headModel.state integerValue]  == 0) {
            //未处理
            CStousuConLisModel *model = [[CStousuConLisModel alloc]init];
            model.mytime = self.headModel.createDate;
            model.steContent = @"提交投诉";
            [self.mydateSource addObject:model];
            
        } else if ([self.headModel.state integerValue]  == 1) {
            //处理中
            CStousuConLisModel *model = [[CStousuConLisModel alloc]init];
            model.mytime = self.headModel.knowDate;
            model.steContent = @"区域负责人已知晓投诉";
            [self.mydateSource addObject:model];
            
            CStousuConLisModel *model1 = [[CStousuConLisModel alloc]init];
            model1.mytime = self.headModel.createDate;
            model1.steContent = @"提交投诉";
            [self.mydateSource addObject:model1];
            
        } else {
            //已处理
            CStousuConLisModel *model = [[CStousuConLisModel alloc]init];
            model.mytime = self.headModel.handleDate;
            model.steContent = @"区域负责人给出处理结果";
            model.mycontent = self.headModel.handleContent;
            
            [self.mydateSource addObject:model];
            
            CStousuConLisModel *model1 = [[CStousuConLisModel alloc]init];
            model1.mytime = self.headModel.knowDate;
            model1.steContent = @"区域负责人已知晓投诉";
            [self.mydateSource addObject:model1];
            
            CStousuConLisModel *model2 = [[CStousuConLisModel alloc]init];
            model2.mytime = self.headModel.createDate;
            model2.steContent = @"提交投诉";
            [self.mydateSource addObject:model2];
        }
        
        //添加时间线
        UIView *footerV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT-200-self.mydateSource.count*90-kNaviHeight)];
        UIView *lineV = [UIView new];
        lineV.backgroundColor = [UIColor colorWithHexString:@"#DBDBDB"];
        lineV.frame = CGRectMake(24, 0, 1, footerV.size.height);
        [footerV addSubview:lineV];
        self.myTableView.tableFooterView = footerV;
        
        [self.myTableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
        
    } fail:^(NSError *error) {
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
        
    } needUser:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.mydateSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CSTSConListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CSTSConListTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CStousuConLisModel *model = _mydateSource[indexPath.row];
    [cell reash:model];
    
    if (indexPath.row == 0) {
        cell.timeLB.textColor = cell.cenLb.textColor = cell.stateLB.textColor = cell.contentLb.textColor = cell.huifuLB.textColor = [UIColor colorWithHexString:@"#70BE68"];
        cell.myimage.image = imgname(@"lvdian");
        cell.stateLB.hidden = NO;
        if ([self.headModel.state integerValue]  == 0) {
            //未处理
            cell.stateLB.text =@"未处理";
        } else if ([self.headModel.state integerValue]  == 1) {
            //处理中
            cell.stateLB.text =@"处理中";
        } else {
            //已处理
            cell.stateLB.text =@"已处理";
        }
    }else{
        cell.timeLB.textColor = cell.cenLb.textColor = cell.stateLB.textColor = cell.contentLb.textColor = cell.huifuLB.textColor = K666666;
        cell.stateLB.hidden = YES;
        cell.myimage.image = imgname(@"heidian");
    }
    return cell;
}

-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT-kNaviHeight) style:(UITableViewStylePlain)];
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.estimatedRowHeight = 90;
        _myTableView.bounces = NO;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.tableFooterView = [UIView new];
        //        _myTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:_myTableView.bounds image:imgname(@"tableviewPlaseholder") viewClick:^{
        //            [_myTableView.mj_header beginRefreshing];
        //        }];
        [_myTableView registerNib:[UINib nibWithNibName:@"CSTSConListTableViewCell" bundle:nil] forCellReuseIdentifier:@"CSTSConListTableViewCell"];
        adjustInset(_myTableView);
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}

@end
